﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevEx.Thanh.reports;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraReports.UI;

namespace DevEx.Thanh.UserControl
{
    public partial class ucQuanLyHopDong : XtraUserControl
    {
        QLVTDataContext dc = new QLVTDataContext();
        public ucQuanLyHopDong()
        {
            InitializeComponent();
        }

        public void KhoiTaodtgHopDong(String Sname)
        {
            var q = from p in dc.tblHopDongs select new { p.ID_HopDong, p.SoHopDong, p.NgayKy, p.TongGiaTri, p.ID_DuAn };
            dtgHopDong.DataSource = q;

            ////focus
            //int focusrow = 0;
            //if (Sname != "")
            //{
            //    for (int i = 0; i < gridView1.RowCount; i++)
            //    {
            //        if (gridView1.GetRowCellValue(i, "SoHopDong").ToString().ToUpper() == Sname.ToUpper())
            //        {
            //            focusrow = i;
            //        }
            //    }
            //}

            //gridView1.FocusedRowHandle = focusrow;

            FormatdtgHopDong();
        }

        public void FormatdtgHopDong()
        {
            gridView1.Columns.ColumnByFieldName("ID_HopDong").Visible = false;
            gridView1.Columns.ColumnByFieldName("ID_DuAn").Visible = false;
            //gridView1.Columns.ColumnByFieldName("NgayHopDong")


            var cl_VT = gridView1.Columns.ColumnByFieldName("SoHopDong");
            cl_VT.Width = 150;
            cl_VT.Caption = "Số Hợp đồng";
            cl_VT.OptionsColumn.AllowEdit = false;
            cl_VT.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_VT.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns NgayPhieu
            var cl_NgayPhieu = gridView1.Columns.ColumnByFieldName("NgayKy");
            cl_NgayPhieu.Width = 100;
            cl_NgayPhieu.Caption = "Ngày HĐ";
            cl_NgayPhieu.OptionsColumn.AllowEdit = false;
            cl_NgayPhieu.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_NgayPhieu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;


            //Columns TongGiaTri
            var cl_TongGiaTri = gridView1.Columns.ColumnByFieldName("TongGiaTri");
            //cl_TongGiaTri.Width = ;
            cl_TongGiaTri.Caption = "Tổng giá trị";
            cl_TongGiaTri.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_TongGiaTri.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_TongGiaTri.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_TongGiaTri.DisplayFormat.FormatString = "#,###.##";
            //Font f = new Font(this.Font, FontStyle.Bold);
            //cl_TongGiaTri.AppearanceCell.Font = f;
        }

        private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            GridView view = (GridView)sender;
            //Check whether the indicator cell belongs to a data row
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (Convert.ToInt32(e.RowHandle) + 1).ToString();
                //if (!indicatorIcon)
                e.Info.ImageIndex = -1;
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            dtgPhieuNhapXuat.DataSource = null;
            //hiển thị các phiếu nhập xuất liên quan theo hợp đồng
            int idHopDong = (int)gridView1.GetFocusedRowCellValue("ID_HopDong");
            KhoiTaodtgPhieuNhapXuat(idHopDong);
        }

        private void ucQuanLyHopDong_Load(object sender, EventArgs e)
        {
            KhoiTaodtgHopDong("");
            FormatdtgPhieuNhapXuat();
        }
        public void KhoiTaodtgPhieuNhapXuat(int ID_HopDong)
        {
            var q = (from p in dc.tblDPNXes
                    join ct in dc.tblSLNXes on p.ID_DPNX equals ct.ID_DPNX
                    join t in dc.tblTons on ct.ID_MatHang equals t.ID_MatHang
                    where t.ID_HopDong == ID_HopDong && p.NX =="N"
                    select new
                    {
                        t.tblVatTu.VatTu,
                        t.tblVatTu.MaKyHieu,
                        t.tblVatTu.tblDVT.DVT,
                        t.Gia,
                        ct.SLKH,
                        ct.ThanhTien,
                        t.tblNguonVatTu.KH_NguonVatTu,
                        SoPhieu = (Convert.ToDateTime(p.NgayPhieu).Month > 9 ?
                           Convert.ToString(p.SoPhieu) + p.DuoiPhieu + "/" + Convert.ToDateTime(p.NgayPhieu).Month.ToString() + "-" + Convert.ToDateTime(p.NgayPhieu).Year.ToString().Substring(2, 2)
                           : Convert.ToString(p.SoPhieu) + p.DuoiPhieu + "/0" + Convert.ToDateTime(p.NgayPhieu).Month.ToString() + "-" + Convert.ToDateTime(p.NgayPhieu).Year.ToString().Substring(2, 2)),
                        p.NgayPhieu
                    })
            .GroupBy(g => new { g.VatTu,g.MaKyHieu, g.DVT, g.Gia, g.SLKH, g.ThanhTien, g.KH_NguonVatTu,g.SoPhieu ,g.NgayPhieu })
            .Select(s => s.FirstOrDefault());

            dtgPhieuNhapXuat.DataSource = q;
            //FormatdtgPhieuNhapXuat();
        }
        public void FormatdtgPhieuNhapXuat()
        {
            //col so phieu
            var cl_Sp = gridView2.Columns.ColumnByFieldName("SoPhieu");
            cl_Sp.Width = 50;
            cl_Sp.Caption = "P.Xuất";
            cl_Sp.OptionsColumn.AllowEdit = false;
            cl_Sp.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_Sp.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns NgayPhieu
            var cl_NgayPhieu = gridView2.Columns.ColumnByFieldName("NgayPhieu");
            cl_NgayPhieu.Width = 50;
            cl_NgayPhieu.Caption = "Ngày tháng";
            cl_NgayPhieu.DisplayFormat.FormatType = FormatType.DateTime;
            cl_NgayPhieu.DisplayFormat.FormatString = "dd/MM/yyyy";
            cl_NgayPhieu.OptionsColumn.AllowEdit = false;
            cl_NgayPhieu.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_NgayPhieu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //col ten vat tu
            var cl_VT = gridView2.Columns.ColumnByFieldName("VatTu");
            cl_VT.Width = 150;
            cl_VT.Caption = "Tên vật tư";
            cl_VT.OptionsColumn.AllowEdit = false;
            cl_VT.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_VT.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Near;

            //col Ma ky hieu
            var cl_Ma = gridView2.Columns.ColumnByFieldName("MaKyHieu");
            cl_Ma.Width = 100;
            cl_Ma.Caption = "Nhãn hiệu, quy cách";
            cl_Ma.OptionsColumn.AllowEdit = false;
            cl_Ma.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_Ma.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Near;

            //col DVT
            var cl_Dvt = gridView2.Columns.ColumnByFieldName("DVT");
            cl_Dvt.Width = 40;
            cl_Dvt.Caption = "ĐVT";
            cl_Dvt.OptionsColumn.AllowEdit = false;
            cl_Dvt.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_Dvt.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns Gia
            var cl_Gia = gridView2.Columns.ColumnByFieldName("Gia");
            cl_Gia.Width = 50;
            cl_Gia.Caption = "Đơn giá";
            cl_Gia.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_Gia.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_Gia.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_Gia.DisplayFormat.FormatString = "#,###.##";

            //Columns So luong
            var cl_SL = gridView2.Columns.ColumnByFieldName("SLKH");
            cl_SL.Width = 50;
            cl_SL.Caption = "Số lượng";
            cl_SL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_SL.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_SL.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_SL.DisplayFormat.FormatString = "#,###.##";
            //Columns ThanhTien
            var cl_TongTien = gridView2.Columns.ColumnByFieldName("ThanhTien");
            cl_TongTien.Width = 80;
            cl_TongTien.Caption = "Thành tiền";
            cl_TongTien.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_TongTien.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_TongTien.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_TongTien.DisplayFormat.FormatString = "#,###.##";
            cl_TongTien.Summary.Add(DevExpress.Data.SummaryItemType.Sum, "ThanhTien", "{0:n0}");

            //col Nguon
            var cl_Nguon = gridView2.Columns.ColumnByFieldName("KH_NguonVatTu");
            cl_Nguon.Width = 50;
            cl_Nguon.Caption = "Nguồn";
            cl_Nguon.OptionsColumn.AllowEdit = false;
            cl_Nguon.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_Nguon.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
        }
        private void btInDanhSach_Click(object sender, EventArgs e)
        {
            //lấy dữ liệu đổ ra dataset
            DateTime tuNgay = new DateTime(DateTime.Now.Year, 1, 1);
            DateTime denNgay = new DateTime(DateTime.Now.Year, 12, 31);
            DataTable dt = new DataTable();
            string cauLenh = "select * from fc_T_LayThongTinNhapXuat('" + tuNgay.ToString("yyyy-MM-dd") + "','" + denNgay.ToString( "yyyy-MM-dd") + "') where NX ='N'";
            DevEx.Thanh.Utilities.XL_DULIEU.LayDuLieuRaBang(cauLenh , ref dt);
            dt.TableName = "fc_T_LayThongTinNhapXuat";DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            //in báo cáo
            rptQLyHopDong rpt = new rptQLyHopDong();
            rpt.DataSource = ds;
            //set gia tri cho no
            rpt.RequestParameters = false; //hiển thị report luôn, không cần chờ nhập parametter
            ReportPrintTool pt = new ReportPrintTool(rpt);
            pt.AutoShowParametersPanel = false; //giấu phần hiển thị cho nhập parametter
            pt.ShowPreviewDialog();
        }
    }
}
